﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * URL路由实体类。
 * 
 * 最后修改：2011-10-28
 * ************************************************************/
namespace Mozlite.Common
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.Routing;
    using System.Text.RegularExpressions;

    /// <summary>
    /// URL路由实体类。
    /// </summary>
    public class Route
    {
        /// <summary>
        /// 初始化类<see cref="T:Mozlite.Common.Route"/>。
        /// </summary>
        public Route() { }
        /// <summary>
        /// 初始化类<see cref="T:Mozlite.Common.Route"/>。
        /// </summary>
        /// <param name="reader">数据库读取器。</param>
        public Route(System.Data.IDataReader reader)
        {
            this.RouteID = reader.GetInt32("RouteID");
            this.AreaName = reader.GetString("AreaName");
            this.Key = reader.GetString("Key");
            this.Url = reader.GetString("Url");
            this.Defaults = reader.GetString("Defaults");
            this.Constraints = reader.GetString("Constraints");
            this.RouteHandlerString = reader.GetString("RouteHandler");
        }

        /// <summary>
        /// 路由ID。
        /// </summary>
        public int RouteID { get; set; }
        /// <summary>
        /// 区域名称。
        /// </summary>
        public string AreaName { get; set; }
        /// <summary>
        /// 区域唯一键。
        /// </summary>
        public string Key { get; set; }
        /// <summary>
        /// 获取或设置URL地址。
        /// </summary>
        public string Url { get; set; }

        private IRouteHandler handler = null;
        /// <summary>
        /// 获取当前路由处理器。
        /// </summary>
        /// <returns>返回当前路由处理器。</returns>
        public IRouteHandler RouteHandler
        {
            get
            {
                if (handler == null)
                {
                    if (!string.IsNullOrEmpty(RouteHandlerString))
                    {
                        try
                        {
                            Type type = Type.GetType(RouteHandlerString);
                            if (type != null)
                            {
                                handler = Activator.CreateInstance(type) as IRouteHandler;
                            }
                        }
                        catch { }
                    }
                    if (handler == null)
                        handler = new System.Web.Mvc.MvcRouteHandler();
                }
                return handler;
            }
        }
        /// <summary>
        /// 获取或设置路由处理器类型字符串。
        /// </summary>
        public string RouteHandlerString { get; set; }

        /// <summary>
        /// 获取或设置默认值。
        /// </summary>
        public string Defaults { get; set; }
        /// <summary>
        /// 获取或设置正则表达式约束。
        /// </summary>
        public string Constraints { get; set; }
        /// <summary>
        /// 从字符串中分析为字典实例对象。
        /// </summary>
        /// <param name="str">当前字符串。</param>
        /// <returns>返回实例对象。</returns>
        public static RouteValueDictionary FromString(string str) {
            if (str == null)
                return null;
            RouteValueDictionary routes = new RouteValueDictionary();
            int index;
            string key, value;
            foreach (var ds in str.Split<string>("\r\n")) {
                index = ds.IndexOf('=');
                if (index == -1)
                    continue;
                key = ds.Substring(0, index).Trim();
                value = ds.Substring(index + 1).Trim();
                routes.Add(key, value);
            }
            return routes;
        }
        /// <summary>
        /// 判断当前属性是否为空。
        /// </summary>
        public bool IsNullOrEmpty {
            get {
                return string.IsNullOrEmpty(this.Url) || string.IsNullOrEmpty(Defaults) || string.IsNullOrEmpty(this.Key);
            }
        }
    }
}
